package shopping;

import org.w3c.dom.ls.LSOutput;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        Product p1 = new Product("苹果", "水果", 8);
        Product p2 = new Product("西瓜汁", "饮品", 25);
        Product p3 = new Product("可乐", "饮品", 3);

        Product[] arr = new Product[3];
        arr[0] = p1;
        arr[1] = p2;
        arr[2] = p3;

        ShoppingCart cart = new ShoppingCart();//定义一个购物车

        Product[] pp=new Product[10];


        label:
        while(true){
        System.out.println("请输入您想进行的操作");
        String opt = scanner.nextLine();
            switch (opt) {
                case "add":
                    add(pp, arr);

                    break;
                case "remove":
                    remove(pp);

                    break;
                case "display":
                    fixed(pp, cart);
                    display(pp);

                    break;
                case "print":
                    fixed(pp, cart);
                    print(pp);
                    break;
                case "over":
                    break label;
                default:
                    System.out.println("该操作不存在");

            }
       }


    }
    //方法fixed：把一个商品数组赋给购物车
    public static void fixed(Product[] product,ShoppingCart cart) {
        for(int i=0;i<product.length;i++){
            if(product[i]==null&&i==0){
                System.out.println("购物车中没有商品");
                break;
            }
            else{
                cart.setProducts(product);
            }
        }

    }
    //方法add:对一个商品数组添加
    public static void add(Product[] product1,Product[] product2){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入添加商品的名称");
        String name = scanner.nextLine();

        for(int i=0,j=0;i<product2.length&&j<product2.length;i++){
            if(product2[i].getName().equals(name)){
                if(product1[j]==null){
                    product1[j] = product2[i];
                    System.out.println("请输入添加该商品的数量");
                    product1[j].setQuantity(scanner.nextInt());
                    break;
                }
                else{j++;i--;}

            }
        }
    }
    //方法remove：对一商品数组删除
    public static void remove(Product[] product){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除的商品名称");
        String name = scanner.nextLine();
        for(int i=0;i<product.length;i++){
            int count=-1;
            if(product[i].getName().equals(name)){
                count=i;
                for(int j=0;j<product.length;j++) {
                    if(j>=count&&j!=product.length-1&&product[j+1]!=null) {
                        product[j]=product[j+1];

                    }
                    else if(j >= count && j == 0){
                        product[j]=null;
                    }
                    else if(j==product.length-1&&product[j]!=null) {
                        product[j]=null;
                    }
                    else{

                    }
                }

            }
            else{
                System.out.println("不含该商品");
            }
        }
    }

    //方法display:对商品数组遍历
    public static void display(Product[] product){
        for(int i=0;i<product.length;i++){
            if(product[i]!=null){
                System.out.println("名称"+"  "+"数量");
                System.out.println(product[i].getName()+"  "+product[i].getQuantity());
            }
            else{
                break;
            }
        }
    }

    //方法print：计算商品数组的总价
    public static void print(Product[] product){
        double total=0;
        for(int i=0;i<product.length;i++){
            if(product[i]!=null){
                total+=product[i].getPrice()*product[i].getQuantity();

            }
            else{
                break;
            }

        }
        System.out.println("总价为 "+total);
    }






}
